package jwcb.admin.server.user.controller;

import jwcb.admin.server.user.domain.dto.*;
import jwcb.admin.server.user.domain.vo.SysUserLoginVO;
import jwcb.admin.server.user.domain.vo.SysUserVo;
import jwcb.admin.server.user.service.ISysUserService;
import jwcb.javapjframework.common.core.utils.BeanCopyUtil;
import jwcb.javapjframework.common.domain.domain.R;
import jwcb.javapjframework.common.domain.domain.vo.TokenVO;
import jwcb.javapjframework.common.security.domain.dto.TokenDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * B端用户控制器
 */
@RestController
@RequestMapping("/sys_user")
public class SysUserController {


    private static final Logger log = LoggerFactory.getLogger(SysUserController.class);
    @Autowired
    private ISysUserService sysUserService;


    /**
     * B端用户登录
     * @param passwordLoginDto 账号密码信息
     * @return token信息
     */
    @RequestMapping("/login")
    public R<TokenVO> login(@Validated @RequestBody PasswordLoginDto passwordLoginDto) {
        TokenDto tokenDto = sysUserService.login(passwordLoginDto);
        TokenVO tokenVO = new TokenVO();
        BeanCopyUtil.copyProperties(tokenDto,tokenVO);
        return R.ok(tokenVO);
    }


    /**
     * 新增用户信息
     * @param sysUserAddDto B端用户信息DTO
     * @return 用户Id
     */
    @PostMapping("/add")
    public R<Long> add(@Validated @RequestBody SysUserAddDto sysUserAddDto) {
        return R.ok(sysUserService.add(sysUserAddDto));
    }

    /**
     * 编辑用户信息
     * @param sysUserEditDto B端用户信息DTO
     * @return 用户Id
     */
    @PostMapping("/edit")
    public R<Long> edit(@Validated @RequestBody SysUserEditDto sysUserEditDto) {
        return R.ok(sysUserService.edit(sysUserEditDto));
    }

    /**
     * 查询B端用户
     * @param sysUserListReqDTO 用户查询DTO
     * @return B用户列表
     */
    @PostMapping("/list")
    public R<List<SysUserVo>> getUserList(@RequestBody SysUserListReqDTO sysUserListReqDTO) {
        log.info("getUserList");
        List<SysUserDTO> sysUserDTOS = sysUserService.getUserList(sysUserListReqDTO);
        return R.ok(BeanCopyUtil.copyListProperties(sysUserDTOS,SysUserVo::new));
    }

    /**
     * 获取B端登录用户信息
     * @return B端用户信息VO
     */
    @GetMapping("/login_info/get")
    public R<SysUserLoginVO> getLoginUser() {
        SysUserLoginDto sysUserLoginDto = sysUserService.getLoginUser();
        SysUserLoginVO sysUserLoginVO = new SysUserLoginVO();
        BeanCopyUtil.copyProperties(sysUserLoginDto,sysUserLoginVO);
        return R.ok(sysUserLoginVO);
    }
}
